﻿using CloudMusic.Model;
using LiteDB;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace CloudMusic.Helper
{
    public class DataServiceHelper
    {
        private readonly string _path = "Data.db";

        #region Music
        public IEnumerable<MusicModel> GetAllMusic()
        {
            using (var db = new LiteDatabase(_path))
            {
                return db.GetCollection<MusicModel>("musics").Find(Query.All());
            }
        }

        public void SaveAllMusic(List<MusicModel> musics)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<MusicModel>("musics");
                col.InsertBulk(musics);
            }
        }

        public void RemoveMusic(MusicModel history)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<MusicModel>("musics");
                col.Delete(Query.EQ("Id", history.Id));
            }
        }

        public void AddMusic(MusicModel musics)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<MusicModel>("musics");
                var item = col.FindOne(Query.EQ("Id", musics.Id));
                if (item == null) col.Insert(musics);
            }
        }

        public void ClearAllMusic()
        {
            using (var db = new LiteDatabase(_path))
            {
                db.DropCollection("musics");
            }
        }

        public void ClearLocalMusic()
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<MusicModel>("musics");
                col.Delete(Query.EQ("Type", 0));
            }
        }
        #endregion

        #region History
        public void AddHistory(History history)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<History>("histories");
                var item = col.FindOne(Query.EQ("Keyword", history.Keyword));
                if (item == null) col.Insert(history);
            }
        }

        public IEnumerable<History> GetAllHistory()
        {
            using (var db = new LiteDatabase(_path))
            {
                return db.GetCollection<History>("histories").Find(Query.All("CreateTime", Query.Descending));
            }
        }

        public void RemoveHistory(History history)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<History>("histories");
                col.Delete(Query.Contains("Keyword", history.Keyword));
            }
        }

        public void ClearAllHistory()
        {
            using (var db = new LiteDatabase(_path))
            {
                db.DropCollection("histories");
            }
        }
        #endregion

        #region user
        /// <summary>
        /// 获取存储的用户信息
        /// </summary>
        /// <returns></returns>
        public IEnumerable<userInfo> GetUserInfo()
        {
            using (var db = new LiteDatabase(_path))
            {
                return db.GetCollection<userInfo>("user").Find(Query.All());
            }
        }
        /// <summary>
        /// 存储用户信息
        /// </summary>
        /// <param name="user"></param>
        public void SaveUserInfo(List<userInfo> user)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<userInfo>("user");
                col.InsertBulk(user);
            }
        }
        /// <summary>
        /// 更新用户登录信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int UpdataLoginState(List<userInfo> obj) 
        {
            using (var db = new LiteDatabase(_path))
            {
                var findResult = db.GetCollection<userInfo>("user").Update(obj);
                return findResult;
            }
          
        }
        #endregion

        #region like
        /// <summary>
        /// 保存ids
        /// </summary>
        /// <param name="like"></param>
        public void SaveLikesInfo(List<LikesIdModel> like)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<LikesIdModel>("like");
                col.InsertBulk(like);
            }
        }
        /// <summary>
        /// 获取ids
        /// </summary>
        /// <returns></returns>
        public IEnumerable<LikesIdModel> GetIdsInfo()
        {
            using (var db = new LiteDatabase(_path))
            {
                return db.GetCollection<LikesIdModel>("like").Find(Query.All());
            }
        }
        #endregion

        #region 通用

        public int Save<T>(T obj,string key) where T : new()
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<T>(key);
                var value = col.Insert(obj);
                return value.AsInt32;
            }
        }
        public  bool Update<T>(T obj, string key) where T : new()
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<T>(key);
                var success = col.Update(obj);
                return success;
            }
        }
        public  bool Delete(int docId, string key)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection(key).Delete(docId);
               // var success = col.Delete(docId);
                return col;
            }
        }
        public BsonDocument FindById(int docId, string key)
        {
            using (var db = new LiteDatabase(_path))
            {
                var doc = db.GetCollection(key).FindById(docId);
                //var doc = col.FindById(docId);
                return doc;
            }
        }
        public T FindById<T>(int docId,string key) where T : new()
        {
            using (var db = new LiteDatabase(_path))
            {
                var doc = db.GetCollection(key).FindById(docId);
                //var doc = col.FindById(docId);
                return BsonToObject.ConvertTo<T>(doc);
            }
        }
        public IList<BsonDocument> FindAll(string key)
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection(key);
                var doc = col.FindAll().ToList();
                return doc;
            }
        }
        public  IList<T> FindAll<T>(string key) where T : new()
        {
            using (var db = new LiteDatabase(_path))
            {
                var col = db.GetCollection<T>(key);
                var docs = col.FindAll();
                return docs.ToList();
            }
        }


        #endregion 
    }
    /// <summary>
    /// 数据转换
    /// </summary>
    public class BsonToObject
    {
        public static T ConvertTo<T>(BsonDocument bd)
            where T : new()
        {
            T model = new T();
            PropertyInfo[] propertyInfos = model.GetType().GetProperties();
            foreach (var property in propertyInfos)
            {
                if (property != null && bd.ContainsKey(property.Name))
                {
                    property.SetValue(model, bd[property.Name], null);
                }
            }
            return model;
        }
        public static IList<T> ConvertToList<T>(List<BsonDocument> dbList)
            where T : new()
        {
            IList<T> _List = new List<T>();
            foreach (var bd in dbList)
            {
                T _t = ConvertTo<T>(bd);
                if (_t != null)
                {
                    _List.Add(_t);
                }
            }
            return _List;
        }
    }
}
